package edu.java8;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class StreamDemo {

    public static void main(String[] args) {
        List<Integer> list = Arrays.asList(100, 90, 89, 78, 56, 28, 91);
//        filter(list);
//        map(list);
        sortAndLimit(list);
    }

    static void collects(List<Integer> list){
        //distinct 去重
        list.stream()
                .distinct();
    }

    //将成绩排序   取出前五名
    static void sortAndLimit(List<Integer> list){
        List<Integer> collect = list.stream()
                .sorted((o1, o2) -> o2.compareTo(o1))
                .limit(5)
                .collect(Collectors.toList());
        System.out.println("collect = " + collect);

    }

    static void map(List<Integer> list){
        //map  映射 将每个元素进行映射后操作
        List<Double> collect = list.stream()
                .map(i -> i * 0.6)
                .collect(Collectors.toList());
        System.out.println("collect = " + collect);
    }

    //过滤数据  >= 60
    static void filter(List<Integer> list){
        //filter 过滤   collect 搜集 实现规约操作
        List<Integer> collect = list.stream()
                .filter(i -> i >= 60)
                .collect(Collectors.toList());
        System.out.println(collect);
    }


    static void forEach(){
//        数据源： list、set、map
        List<Integer> list = Arrays.asList(100, 90, 89, 78, 56, 28, 91);
//        stream()  将数据源转成流
        list.stream().forEach(i -> System.out.println(i));
    }

}
